/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import general.Agent;
import general.Commands;
import general.MessageHandler;
import general.RequestBuilder;
import general.UDPConnection;
import general.UDPMessage;
import general.UDPTestConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 *
 * @author foliveira
 */
public final class UDPOrgao extends Agent {

    private List<String> listLB;
    private Map<String, Double> mapEmployee;
    private UDPTestConnection tConnection;
    private UDPConnection connection;
    private MessageHandler handler;
    private UDPMessage message;
    private RequestBuilder request;
    private String currentServer;

    public UDPOrgao(String name) {

        this.setName(name);
        connection = new UDPConnection();
        listLB = new ArrayList<>();

        getLoadBalanceEnable();
        getServerAvaliable();        
        whaitForMessage();



    }

    private void whaitForMessage() {


        while (true) {

            try {

                message = new UDPMessage(connection);
                String m = message.receive();
                request = new RequestBuilder(m);

                if (request.getRequest().getCommand().contains("DATASERVICE")) {
                    this.currentServer = request.getRequest().getData();
                } else if (request.getRequest().getMessageReturn().contains("DENIED")) {
                }



            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }

    }

    /*
     * Procura um loadBalance disponível
     */
    public void getLoadBalanceEnable() {

        List<String> list = new ArrayList<>();

        list.add("localhost:9091");
        list.add("localhost:9092");
        list.add("localhost:9093");
        list.add("localhost:9094");
        list.add("localhost:9095");


        for (Iterator<String> it = list.iterator(); it.hasNext();) {
            String host = it.next();


            tConnection = new UDPTestConnection(host);

            if (tConnection.connected()) {
                listLB.add(host);
            }


        }

    }

    /*
     * Pergunta aos LoadBalance ativos quem pode atender a uma requisção
     */
    private void getServerAvaliable() {

        for (Iterator<String> it = listLB.iterator(); it.hasNext();) {

            String host = it.next();
            tConnection = new UDPTestConnection(host);

            if (tConnection.connected()) {

                String msg = String.format("%s:%s:%s:%s:%s:%s", 2, Commands.WHO, 0, "USER", "PASSWD", 0);


                try {

                    request = new RequestBuilder(msg);
                    message = new UDPMessage(connection);
                    message.to(tConnection.getHost(), tConnection.getPort());
                    message.send(request.getRequest().toString());

                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                break;  // achando o primeiro abandona 

            }


        }


    }

    public String getCurrentServer() {
        return currentServer;
    }

    public void setCurrentServer(String currentServer) {
        this.currentServer = currentServer;
    }

    private void getCommands() {



        Scanner sc = new Scanner(System.in);
        System.out.println("Informe o comando");

        String msg = String.format("%s:%s:%s:%s:%s:%s", 2, Commands.WHO, 0, "USER", "PASSWD", 0);

        
        

    }
}
